/*
 * LogManager.java
 *
 *  
 */
package org.msb.finance.app;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Manages a cache of {@link Logger} objects defined for this application. The {@link Logger} objects are referenced by
 * an alias which is simply the part of its full name that follows {@code org.msb.finance}.
 * 
 * @author Marc Boudreau
 * 
 */
public class LogManager {

	/**
	 * The cache of previously retrieved {@link Logger} objects.
	 */
	private Map<String, Logger> loggers;

	/**
	 * The default instance of this class.
	 */
	private static LogManager instance = new LogManager();

	/**
	 * Constructs a new {@code LogManager} instance.
	 */
	public LogManager() {
		this.loggers = new HashMap<String, Logger>();
	}

	/**
	 * Retrieves a {@link Logger} object defined for this application by its alias.
	 * 
	 * @param name
	 *            A {@link String} object containing the alias of the {@link Logger} to retrieve.
	 * 
	 * @return The {@link Logger} object that corresponds to the provided alias.
	 */
	public Logger getLogger(String name) {
		Logger logger = this.loggers.get(name);
		if (null == logger) {
			logger = java.util.logging.LogManager.getLogManager().getLogger("org.msb.finance." + name); //$NON-NLS-1$
			this.loggers.put(name, logger);
		}

		return logger;
	}

	/**
	 * Returns the default instance of this class.
	 * 
	 * @return The default {@code LogManager} object.
	 */
	public static LogManager getLogManager() {
		return LogManager.instance;
	}
}
